<script setup lang="ts">
import { ref } from 'vue'
import { showToast } from 'vant'
import type { UploaderAfterRead } from 'vant/lib/uploader/types'
import { uploadImgAPI } from '@/services'
import type { Image } from '@/types/consult'
defineProps<{
  disabled?: boolean
}>()

// 双向的输入框内容
const inputText = ref('')

// 子组件向父组件触发事件
const emit = defineEmits<{
  (e: 'send-text', msg: string): void
  (e: 'send-imag', img: Image): void
}>()

// 发送消息
const handelKeyup = () => {
  // 做判断如果输入框没有值给提示
  if (!inputText.value) return showToast('请输入内容')
  // emit触发事件
  emit('send-text', inputText.value)
  // 清空输入框内容
  inputText.value = ''
}

// 上传图片
const handelAfterRead: UploaderAfterRead = async (list) => {
  if (Array.isArray(list)) return
  if (!list.file) return
  const res = await uploadImgAPI(list.file)
  emit('send-imag', res.data)
}
</script>

<template>
  <div class="room-action">
    <van-field
      v-model="inputText"
      @keyup.enter="handelKeyup"
      type="text"
      class="input"
      :border="false"
      placeholder="问医生"
      autocomplete="off"
      :disabled="disabled"
    ></van-field>
    <van-uploader
      :after-read="handelAfterRead"
      :preview-image="false"
      :disabled="disabled"
    >
      <cp-icons name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;
  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }
  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
